import {
  electronApiInvokeType,
  electronApiSendType,
  renderElectronApiInvoke,
  renderElectronApiSend,
} from "../main/render";

const { contextBridge, ipcRenderer } = require("electron");
const electronApiSend = (): electronApiSendType => {
  let obj = {};
  for (const key in renderElectronApiSend) {
    obj[key] = (...args) => ipcRenderer.send(key, ...args);
  }
  return obj as electronApiSendType;
};

const electronApiInvoke = (): electronApiInvokeType => {
  let obj = {};
  for (const key in renderElectronApiInvoke) {
    obj[key] = (...args) => ipcRenderer.invoke(key, ...args);
  }
  return obj as electronApiInvokeType;
};

const combine = { ...electronApiInvoke(), ...electronApiSend() };

export type electronApiType = typeof combine;
contextBridge.exposeInMainWorld("electronAPI", combine);
